package edu.wfu.preparedDishes.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import edu.wfu.preparedDishes.user.entity.AddressEntity;
import edu.wfu.preparedDishes.user.service.AddressService;

import javax.servlet.http.HttpServletRequest;


/**
 * 收货地址表;
 *
 * @author ljh
 * @email hero_miracle@gmail.com
 * @date 2023-11-24 17:02:00
 */
@RestController
@RequestMapping("user/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{addressId}")
    public R info(@PathVariable("addressId") String addressId){
		AddressEntity address = addressService.getById(addressId);
        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address,HttpServletRequest request){
        address.setUserId((String) request.getAttribute("userId"));
		addressService.save(address);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AddressEntity address){
		addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] addressIds){
		addressService.removeByIds(Arrays.asList(addressIds));
        return R.ok();
    }
    /**
     * 根据用户Id查询地址
     */
    @RequestMapping("/queryById")
    public R selectAddressByUserId(@RequestParam String userId,HttpServletRequest request) {
        //从request域中取userId
        userId = (String) request.getAttribute("userId");
        List<AddressEntity> addressEntity = this.addressService.selectAddressByUserId(userId);
        return R.ok().put("addressEntityList", addressEntity);
    }
    /**
     * 根据地址id查询地址信息
     */
    @RequestMapping("/queryByAddressId")
    public R selectAddressByAddressId(@RequestParam String addressId){
        AddressEntity addressEntity = this.addressService.selectAddressByAddressId(addressId);
        return R.ok().put("addressEntity",addressEntity);
    }
}
